/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import types.ISudokuMatrix;
import types.SudokuMatrix;

/**
 *
 * @author Roman
 */
public class SudokuMatrixIO {

  public static ISudokuMatrix getSudokuMatrix(String filename) throws FileNotFoundException {

    StringTokenizer st = null;
    ISudokuMatrix sMatrix = null;
    int count = 0;

    try {
      FileInputStream fstream = new FileInputStream(filename);
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;

     if ((strLine = br.readLine()) != null){
       st = new StringTokenizer(strLine, "\t");
       count = st.countTokens();
       sMatrix = new SudokuMatrix(count,count);

     int row = 0, col = 0;
     do{
       st = new StringTokenizer(strLine, "\t");
       while (st.hasMoreTokens()) {
         if (col < sMatrix.getColCount()) {
          sMatrix.insertElementAt(Integer.valueOf(st.nextToken()).intValue(), row, col);
          ++col;
         }else
         {
           ++row;
           col = 0;
         }
       }
      }while ((strLine = br.readLine()) != null);
     }else{
      return null;
     }
    } catch (IOException ex) {
      Logger.getLogger(SudokuMatrixIO.class.getName()).log(Level.SEVERE, null, ex);
    } 
    return sMatrix;
   }
  public static void saveSudokuMatrix(String filename, ISudokuMatrix matrix ) throws FileNotFoundException, IOException {

    StringTokenizer st = null;
    ISudokuMatrix sMatrix = null;
    BufferedWriter bw = null;

    try {
        FileOutputStream fstream = new FileOutputStream(filename);
        DataOutputStream out = new DataOutputStream(fstream);
        bw = new BufferedWriter(new OutputStreamWriter(out));

      for(int i=0; i<matrix.getRowCount(); i++){
          
          for (int j=0; j<matrix.getColCount(); j++){
              int digit = (Integer)matrix.getElementAt(i, j);
              bw.write(""+digit);
              bw.write("\t");
          }
          System.out.println();
          bw.newLine();
      }
    } catch (IOException ex) {
      Logger.getLogger(SudokuMatrixIO.class.getName()).log(Level.SEVERE, null, ex);
    }finally {
      if( bw != null )
          bw.flush();
          bw.close();
    }

   }
  public static String replaceToUnixSeparator(String fn){
        String curSepar = "\\";
        String newSepar = "/";
        String fn_temp = "";
        if (fn == null)
            return fn_temp;
        if (fn != null || !fn.equals("")){
          fn_temp = fn.replace(curSepar, newSepar);
        }
        return fn_temp;
    }
  public static String replaceToWinSeparator(String fn){
        String curSepar = "/";
        String newSepar = "\\";

        String fn_temp = "";
        if (fn == null)
            return fn_temp;
        if (fn != null ||!fn.equals("")){
          fn_temp = fn.replace(curSepar, newSepar);
        }
        return fn_temp;
    }
}
